﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Display.UpgradesUI {
    import flash.events.*;
    import Engine.Managers.*;
    import flash.display.*;
    import Events.*;
    import Display.DialogUI.*;
    import org.aswing.*;
    import Classes.*;
    import Display.*;
    import flash.text.*;
    import Classes.gates.*;
    import Display.aswingui.*;
    import Modules.stats.types.*;

    public class MunicipalUpgradesDialogView extends GenericDialogView {

        public static const UPDATE_POSITIONS:String = "update_positions";

        protected var m_buildingName:String;
        protected var m_crewData:Array;
        protected var m_buttonsPanel:JPanel;
        protected var m_numbersLabel:JLabel;
        protected var m_finishCheckCB:Function;
        protected var m_finishCB:Function;
        protected var m_buyAllHandler:Function;
        protected var m_scrollingCrew:UpgradesCrewScrollingList;
        protected var m_targetObjectId:Number;

        public function MunicipalUpgradesDialogView(_arg1:String, _arg2:Array, _arg3:Function, _arg4:Function, _arg5:Function, _arg6:Number){
            this.m_buildingName = _arg1;
            this.m_crewData = _arg2;
            this.m_finishCheckCB = _arg3;
            this.m_finishCB = _arg4;
            this.m_buyAllHandler = _arg5;
            this.m_targetObjectId = _arg6;
            super(null);
            this.setLayout(new SoftBoxLayout(SoftBoxLayout.Y_AXIS));
        }
        override protected function init():void{
            m_bgAsset = UpgradesCommonAssets.assetDict["dialogBG"];
            this.makeBackground();
            this.makeCenterPanel();
            ASwingHelper.prepare(this);
        }
        override protected function makeBackground():void{
            var _local1:MarginBackground;
            if (m_bgAsset){
                _local1 = new MarginBackground(m_bgAsset);
                this.setBackgroundDecorator(_local1);
                this.setPreferredWidth(m_bgAsset.width);
                this.setMinimumWidth(m_bgAsset.width);
                this.setMaximumWidth(m_bgAsset.width);
            };
        }
        override protected function makeCenterPanel():void{
            this.append(createHeaderPanel());
            this.append(ASwingHelper.verticalStrut(8));
            this.append(this.createUpgradeInfoPanel());
            this.append(ASwingHelper.verticalStrut(5));
            this.append(this.createPositionsInfoPanel());
            this.append(ASwingHelper.verticalStrut(5));
            this.append(this.createCrewPanel());
            this.append(ASwingHelper.verticalStrut(15));
            this.append(this.createButtonsPanel());
        }
        override protected function createTitlePanel():JPanel{
            var _local1:JPanel;
            _local1 = ASwingHelper.makeSoftBoxJPanel(SoftBoxLayout.CENTER);
            m_titleFontSize = TextFieldUtil.getLocaleFontSize(27, 20, null);
            var _local2:String = ZLoc.t("Dialogs", "UpgradeX", {itemName:ZLoc.t("Items", (this.m_buildingName + "_friendlyName"))});
            _local2 = TextFieldUtil.formatSmallCapsString(_local2);
            title = ASwingHelper.makeTextField((_local2 + " "), EmbeddedArt.TITLE_FONT, m_titleFontSize, EmbeddedArt.titleColor, 3);
            title.filters = EmbeddedArt.newtitleFilters;
            var _local3:TextFormat = new TextFormat();
            _local3.size = this.getTitleTextSizeHeader(_local2.length);
            TextFieldUtil.formatSmallCaps(title.getTextField(), _local3);
            _local1.append(title);
            title.getTextField().height = (m_titleFontSize * 1.5);
            ASwingHelper.setEasyBorder(_local1, 4);
            return (_local1);
        }
        public function getTitleTextSizeHeader(_arg1:int):int{
            if (_arg1 > 30){
                return ((m_titleFontSize * 0.89));
            };
            return (m_titleFontSize);
        }
        override protected function createCloseButtonPanel():JPanel{
            var _local1:JPanel = new JPanel(ASwingHelper.softBoxLayoutVertical);
            var _local2:JButton = ASwingHelper.makeMarketCloseButton();
            _local2.addEventListener(MouseEvent.CLICK, this.onCancelX, false, 0, true);
            _local1.append(_local2);
            ASwingHelper.setEasyBorder(_local2, 3, 0, 0, 5);
            return (_local1);
        }
        override protected function onCancelX(_arg1):void{
            this.m_cancelTextName = "";
            var _local2:MapResource = (Global.world.getObjectById(this.m_targetObjectId) as MapResource);
            _local2.setShowUpgradeArrow(false);
            _local2.setState(_local2.getState());
            var _local3:Item = Global.gameSettings.getItemByName(this.m_buildingName);
            StatsManager.sample(100, StatsCounterType.DIALOG, StatsKingdomType.ACTIVE_BUILDING, StatsPhylumType.X, _local3.upgrade.newItemName);
            onCancel(_arg1);
        }
        protected function createUpgradeInfoPanel():JPanel{
            var _local1:JPanel = ASwingHelper.makeSoftBoxJPanel(SoftBoxLayout.CENTER);
            var _local2:Item = Global.gameSettings.getItemByName(this.m_buildingName);
            var _local3:UpgradesInfoBox = new UpgradesInfoBox(UpgradesCommonAssets.assetDict["manilaCard"], this.m_buildingName);
            var _local4:UpgradesInfoBox = new UpgradesInfoBox(UpgradesCommonAssets.assetDict["manilaCard"], _local2.upgrade.newItemName);
            var _local5:DisplayObject = (new (UpgradesCommonAssets.assetDict["upgradeArrow"])() as DisplayObject);
            var _local6:AssetPane = new AssetPane(_local5);
            var _local7:JPanel = ASwingHelper.makeSoftBoxJPanelVertical(SoftBoxLayout.CENTER);
            _local7.append(_local6);
            _local1.appendAll(_local3, ASwingHelper.horizontalStrut(8), _local7, ASwingHelper.horizontalStrut(8), _local4);
            return (_local1);
        }
        protected function createPositionsInfoPanel():JPanel{
            var _local1:JPanel = ASwingHelper.makeSoftBoxJPanel(SoftBoxLayout.LEFT);
            var _local2:DisplayObject = (new (UpgradesCommonAssets.assetDict["crewIcon"])() as DisplayObject);
            var _local3:AssetPane = new AssetPane(_local2);
            var _local4:JPanel = ASwingHelper.makeSoftBoxJPanelVertical(SoftBoxLayout.TOP);
            var _local5:JPanel = ASwingHelper.makeSoftBoxJPanel(SoftBoxLayout.LEFT);
            var _local6:JPanel = ASwingHelper.makeSoftBoxJPanel(SoftBoxLayout.LEFT);
            var _local7:Number = CrewGate.getFilledPositions(this.m_crewData);
            var _local8:Number = this.m_crewData.length;
            var _local9:String = ((_local7.toString() + "/") + _local8.toString());
            this.m_numbersLabel = new JLabel(_local9);
            this.addEventListener(UPDATE_POSITIONS, this.updatePositionsInfo, false, 0, true);
            this.m_numbersLabel.setFont(ASwingHelper.getTitleFont(26));
            this.m_numbersLabel.setForeground(new ASColor(EmbeddedArt.lightOrangeTextColor));
            var _local10:String = ZLoc.t("Dialogs", "PositionsFilled");
            var _local11:JLabel = new JLabel(TextFieldUtil.formatSmallCapsString(_local10));
            _local11.setFont(ASwingHelper.getBoldFont(12));
            _local11.setForeground(new ASColor(EmbeddedArt.brownTextColor));
            _local5.append(this.m_numbersLabel);
            _local6.append(_local11);
            _local4.appendAll(_local5, ASwingHelper.verticalStrut(-10), _local6);
            _local1.appendAll(ASwingHelper.horizontalStrut(18), _local3, _local4);
            return (_local1);
        }
        protected function updatePositionsInfo(_arg1):void{
            var _local2:Number = CrewGate.getFilledPositions(this.m_crewData);
            var _local3:Number = this.m_crewData.length;
            var _local4:String = ((_local2.toString() + "/") + _local3.toString());
            this.m_numbersLabel.setText(_local4);
            ASwingHelper.prepare(this);
        }
        protected function createCrewPanel():JPanel{
            var _local1:JPanel = ASwingHelper.makeSoftBoxJPanel(SoftBoxLayout.CENTER);
            this.m_scrollingCrew = new UpgradesCrewScrollingList(UpgradesCommonAssets.assetDict, this.m_crewData);
            _local1.append(this.m_scrollingCrew);
            return (_local1);
        }
        protected function createButtonsPanel():JPanel{
            var _local1:AssetIcon;
            var _local2:CustomButton;
            var _local3:CustomButton;
            var _local4:CustomButton;
            if (this.m_buttonsPanel){
                this.m_buttonsPanel.removeAll();
            };
            this.m_buttonsPanel = ASwingHelper.makeSoftBoxJPanel(SoftBoxLayout.CENTER);
            if (!this.m_finishCheckCB()){
                _local1 = new AssetIcon((new EmbeddedArt.icon_cash() as DisplayObject));
                _local2 = new CustomButton(TextFieldUtil.formatSmallCapsString(ZLoc.t("Dialogs", "FillAll")), _local1, "BigCashButtonUI");
                _local2.addEventListener(MouseEvent.CLICK, this.onFillAll, false, 0, true);
                _local2.setPreferredHeight(45);
                _local2.setMinimumHeight(45);
                _local2.setMaximumHeight(45);
                this.m_buttonsPanel.append(_local2);
                this.m_buttonsPanel.append(ASwingHelper.horizontalStrut(20));
            };
            if (this.m_finishCheckCB()){
                _local3 = new CustomButton(TextFieldUtil.formatSmallCapsString(ZLoc.t("Dialogs", "FinishBuilding")), null, "BigGreenButtonUI");
                _local3.addEventListener(MouseEvent.CLICK, this.m_finishCB, false, 0, true);
                _local3.setPreferredHeight(45);
                _local3.setMinimumHeight(45);
                _local3.setMaximumHeight(45);
                this.m_buttonsPanel.append(_local3);
            } else {
                _local4 = new CustomButton(TextFieldUtil.formatSmallCapsString(ZLoc.t("Dialogs", "HireFriendsButtonLabel")), null, "BigGreenButtonUI");
                _local4.addEventListener(MouseEvent.CLICK, this.m_finishCB, false, 0, true);
                _local4.setPreferredHeight(45);
                _local4.setMinimumHeight(45);
                _local4.setMaximumHeight(45);
                this.m_buttonsPanel.append(_local4);
            };
            ASwingHelper.prepare(this.m_buttonsPanel);
            return (this.m_buttonsPanel);
        }
        public function rebuildButtons():void{
            this.remove(this.m_buttonsPanel);
            this.append(this.createButtonsPanel());
            ASwingHelper.prepare(this);
        }
        protected function onFillAll(_arg1:Event):void{
            var e:* = _arg1;
            var baseCost:* = CrewGate.getCrewCost(this.m_buildingName, "pre_upgrade");
            var buyCount:* = (this.m_crewData.length - CrewGate.getFilledPositions(this.m_crewData));
            var totalCost:* = (baseCost * buyCount);
            UI.displayMessage(ZLoc.t("Dialogs", "CrewCashConfirm", {
                crewAmount:buyCount,
                cashAmount:totalCost
            }), GenericDialogView.TYPE_YESNO, function (_arg1:GenericPopupEvent):void{
                if (_arg1.button == GenericDialogView.YES){
                    m_buyAllHandler(m_crewData);
                    m_scrollingCrew.rebuildCellsAfterFillAll();
                    updatePositionsInfo(_arg1);
                    rebuildButtons();
                };
            }, "", true);
        }

    }
}//package Display.UpgradesUI 
